package com.xckj.cycling.utils;

public class DetectUtil {

    public static boolean isValidMac(String macStr) {
        if (macStr == null || macStr.equals("")) {
            return false;
        }
        String macAddressRule = "([A-Fa-f0-9]{2}[-,:]){5}[A-Fa-f0-9]{2}";
        // 这是真正的MAC地址；正则表达式；
        return macStr.matches(macAddressRule);
    }

    /**
     * 判断数据是否合法
     *
     * @param bytes byte[]
     * @return false-不合法
     */
    public static boolean checkDevData(byte[] bytes) {
        int dataLength = bytes.length;
        if (dataLength < 5) {
            return false;
        }
        if (bytes[0] != 0x3A || bytes[1] != 0x0A
                || bytes[dataLength - 2] != 0x0D || bytes[dataLength - 1] != 0x0A) {
            return false;
        }
        if (bytes[3] + 8 != dataLength) {
            return false;
        }
        byte[] dataBytes = getSomeByteData(bytes, 4, bytes[3]);
        byte[] sumCheckBytes = SumCheck(dataBytes, 2);
        if (bytes[dataLength - 4] != sumCheckBytes[1] || bytes[dataLength - 3] != sumCheckBytes[0]) {
            return false;
        }
        return true;
    }

    /**
     * 校验和
     *
     * @param msg    需要计算校验和的byte数组
     * @param length 校验和位数
     * @return 计算出的校验和数组
     */
    private static byte[] SumCheck(byte[] msg, int length) {
        long mSum = 0;
        byte[] mByte = new byte[length];

        /** 逐Byte添加位数和 */
        for (byte byteMsg : msg) {
            long mNum = ((long) byteMsg >= 0) ? (long) byteMsg : ((long) byteMsg + 256);
            mSum += mNum;
        } /** end of for (byte byteMsg : msg) */

        /** 位数和转化为Byte数组 */
        for (int liv_Count = 0; liv_Count < length; liv_Count++) {
            mByte[length - liv_Count - 1] = (byte) (mSum >> (liv_Count * 8) & 0xff);
        } /** end of for (int liv_Count = 0; liv_Count < length; liv_Count++) */

        return mByte;
    }


    public static byte[] HexString2Bytes(String str) {
        str = str.replaceAll(" ", "");
        if (str.length() == 0) {
            return null;
        }
        byte[] bArr = new byte[(str.length() / 2)];
        byte[] bytes = str.getBytes();
        for (int i = 0; i < bytes.length / 2; i++) {
            int i2 = i * 2;
            bArr[i] = uniteBytes(bytes[i2], bytes[i2 + 1]);
        }
        return bArr;
    }

    public static byte uniteBytes(byte b, byte b2) {
        StringBuilder stringBuilder = new StringBuilder();
        String str = "0x";
        stringBuilder.append(str);
        stringBuilder.append(new String(new byte[]{b}));
        b = (byte) (Byte.decode(stringBuilder.toString()) << 4);
        stringBuilder = new StringBuilder();
        stringBuilder.append(str);
        stringBuilder.append(new String(new byte[]{b2}));
        return (byte) (b ^ Byte.decode(stringBuilder.toString()));
    }

    /**
     * 十六进制字节转十六进制字符串
     */
    public static String bytesToHexString(byte[] bArr) {
        StringBuilder stringBuffer = new StringBuilder();
        if (bArr == null || bArr.length <= 0) {
            return null;
        }
        for (byte b : bArr) {
            String toUpperCase = Integer.toHexString(b & 255).toUpperCase();
            if (toUpperCase.length() < 2) {
                stringBuffer.append(0);
            }
            stringBuffer.append(toUpperCase);
        }
        return stringBuffer.toString();
    }

    /**
     * int转十六进制字符串
     *
     * @param i
     * @param bytesLength
     * @return
     */
    public static String intToHexString(int i, int bytesLength) {
        String toHexString = Integer.toHexString(i);
        StringBuilder stringBuffer = new StringBuilder();
        if (bytesLength == 0) {
            bytesLength = 1;
        }
        bytesLength *= 2;
        if (toHexString.length() >= bytesLength) {
            return toHexString.substring(toHexString.length() - bytesLength);
        }
        for (int length = toHexString.length(); length < bytesLength; length++) {
            stringBuffer.append("0");
        }
        return stringBuffer + toHexString;
    }

    /**
     * 2个字节的byte 数组与 int 的相互转换.
     *
     * @param b 2个字节数组
     * @return int
     */
    public static int twoByteArrayToInt(byte[] b) {
        return b[1] & 0xFF |
                (b[0] & 0xFF) << 8;
    }

    /**
     * byte转int.
     *
     * @param byteData 字节
     * @return int
     */
    public static int byteToInt(byte byteData) {
        return byteData & 0xff;
    }

    /**
     * 截取字节数组
     *
     * @param bytes    源数据
     * @param startPos 起始未知
     * @param length   截取的长度
     * @return 截取后的字节数组
     */
    public static byte[] getSomeByteData(byte[] bytes, int startPos, int length) {
        byte[] bufferBytes = new byte[length];
        System.arraycopy(bytes, startPos, bufferBytes, 0, length);
        return bufferBytes;
    }

    /**
     * 切换大小端续
     * 十六进制数组切换大小端
     */
    public static byte[] changeBytes(byte[] a) {
        byte[] b = new byte[a.length];
        for (int i = 0; i < b.length; i++) {
            b[i] = a[b.length - i - 1];
        }
        return b;
    }

    /**
     * 切换大小端续
     * 十六进制字符串切换大小端
     */
    public static String changeBytesByHexStr(String a) {
        byte[] abytes = HexString2Bytes(a);
        byte[] bbytes = changeBytes(abytes);
        return bytesToHexString(bbytes);
    }

}
